package chuishop.yiqimaicai.controller;

import chuishop.yiqimaicai.pojo.GoodsHouse;
import chuishop.yiqimaicai.service.GoodsHouseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class GoodsHouseController {

    @Autowired
    private GoodsHouseService goodsHouseService;
    @RequestMapping("findGoodsHouse")
    @ResponseBody
    public PageInfo<GoodsHouse> findGoodsHouse(Integer pageNo,Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        //启动分页
        PageHelper.startPage(pageNo,pageSize);
        List<GoodsHouse> list=goodsHouseService.findGoodsHouse();
        PageInfo<GoodsHouse> pageInfo=new PageInfo<>(list);

        return pageInfo;
    }

    @RequestMapping("addGoodsHouse")
    @ResponseBody
    public Map<Object,Object> addGoodsHouse(@RequestBody GoodsHouse goodsHouse){

        Map<Object,Object> result=new HashMap<>();
        if(goodsHouseService.addGoodsHouse(goodsHouse)){
            result.put("status", "200");
            result.put("msg", "添加成功");
        } else {
            result.put("status", "500");
            result.put("msg", "添加失败");
        }
        return result;
    }

    @RequestMapping("delGoodsHouseByIds")
    @ResponseBody
    public Map<Object,Object> delGoodsHouseByIds(String ids){

        String strid[]=ids.split(",");
        Map<Object,Object> result=new HashMap<>();

        if(goodsHouseService.delGoodsHouseByIds(strid)){
            result.put("status","200");
            result.put("msg","删除成功");
        }else {
            result.put("status","400");
            result.put("msg","删除失败");
        }
        return result;
    }


    @RequestMapping("updataGoodsHouse")
    @ResponseBody
    public Map<Object,Object> updataGoodsHouse(@RequestBody GoodsHouse goodsHouse){
        Map<Object,Object> result=new HashMap<>();
        if (goodsHouseService.updataGoodsHouse(goodsHouse)){
            result.put("status","200");
            result.put("mag","添加成功");


        }else {
            result.put("status","500");
            result.put("msg","添加失败");
        }
        return result;
    }

    @RequestMapping("findGoodsHouseByname")
    @ResponseBody
    public PageInfo<GoodsHouse> findGoodsHouseByname(String gsname,Integer pageNo,Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        //启动分页
        PageHelper.startPage(pageNo,pageSize);
        List<GoodsHouse> list=goodsHouseService.findGoodsHouseByname(gsname);
        PageInfo<GoodsHouse> pageInfo=new PageInfo<>(list);

        return pageInfo;
    }
}
